<?php
namespace App\Http\Controllers\ApiXcx;

use App\Service\ApiXcx\PointsGoodsService;
use Illuminate\Http\Request;

class PointsGoodsController extends BaseController
{
    //商品

    public function __construct()
    {
        $this->db = new PointsGoodsService();
    }

    //分类列表
    public function getCateList(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $res = $this->db->getCateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //商品列表
    public function getGoodsList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $cateid = $request->post('cate_id', 0);
        $keyword = $request->post('keyword', '');
        $pas['is_sale'] = $request->post('is_sale', 0); //0没有，1降序，2升序
        $pas['is_price'] = $request->post('is_price', 0); //0没有，1降序，2升序
        $pas['is_new'] = $request->post('is_new', 0); //0没有，1上新
        $res = $this->db->getGoodsList($pages, $cateid, $keyword ,$pas, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //商品选项
    public function getOption(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $res = $this->db->getOption();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);

    }

    //商品详情
    public function getGoodsOne(Request $request)
    {
        $token = $request->post('token', '');
        $this->db->addApiCount($token);
        $gid = $request->post('gid', 0);
        if (!$gid) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->getGoodsOne($gid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //SKU列表
    public function getSkuList(Request $request)
    {
        $token = $request->post('token', '');
        $uid = $this->db->addApiCount($token);
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 4);
        $gid = $request->post('gid', 0);
        if (!$gid) {
            return $this->toJson(-2, '商品ID必须的');
        }
        $res = $this->db->getSkuList($pages, $gid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }
}